package net.yeah.mickey_zhouyou.util.fcs.elem;

import java.util.List;

import javax.script.ScriptContext;

import net.yeah.mickey_zhouyou.util.fcs.serialize.IConvertor;

public class ConditionNot extends ConditionSelector {
	private ConditionSelector condition;

	public ConditionNot(ConditionSelector condition) {
		this.condition = condition;
	}

	@Override
	public Object select(Object obj, IConvertor objConvertor,
			ScriptContext context) {
		if (obj == null)
			return null;
		Object notRes = this.condition.select(obj, objConvertor, context);
		if (notRes == null)
			return obj;
		if (obj instanceof List && notRes instanceof List) {
			List<?> l1 = (List<?>) obj;
			List<?> l2 = (List<?>) notRes;
			l1.removeAll(l2);
			return l1;
		}
		if (obj instanceof List)
			return ((List<?>) obj).remove(notRes);
		if (notRes instanceof List) {
			List<?> l = (List<?>) notRes;
			if (l.contains(obj))
				return null;
			return obj;
		}
		if (obj == notRes || obj.equals(notRes))
			return null;
		return obj;
	}
}
